@page "/Organization/Admins"
@using Passwordless.AdminConsole.Identity
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.RateLimiting
@using Passwordless.AdminConsole.EventLog.Loggers
@using Passwordless.AdminConsole.RateLimiting

@attribute [EnableRateLimiting(AdminPageRateLimit.PolicyName)]

@inject ICurrentContext CurrentContext;
@inject IHttpContextAccessor HttpContextAccessor;
@inject IDataService DataService;
@inject IInvitationService InvitationService;
@inject UserManager<ConsoleAdmin> UserManager;
@inject SignInManager<ConsoleAdmin> SigninManager;
@inject IPasswordlessClient PasswordlessClient;
@inject IEventLogger EventLogger;
@inject TimeProvider TimeProvider;
@inject NavigationManager NavigationManager;

<Page Title="Manage Admins">
    <Panel Header="Active Admins">
        @if (ConsoleAdmins != null)
        {
            <EditForm class="space-y-4" FormName="@DeleteActiveFormName" Model="DeleteActiveForm" OnSubmit="@DeleteAdminAsync">
                <CustomValidationErrors EditContext="DeleteActiveFormEditContext" />
                <Table ColumnHeaders="@_activeColumnHeaders" EmptyMessage="@ActiveEmptyMessage">
                    @foreach (var user in ConsoleAdmins)
                    {
                        <tr>
                            <td class="whitespace-nowrap">@user.Name</td>
                            <td class="whitespace-nowrap">@user.Email</td>
                            <td class="whitespace-nowrap">
                                <button class="btn-danger" name="DeleteActiveForm.UserId" value="@user.Id" type="submit">Delete</button>
                            </td>
                        </tr>
                    }
                </Table>
            </EditForm>
        }
    </Panel>
    <Panel Header="Invitations">
        @if (!CanInvite)
        {
            <Alert Style="@ContextualStyles.Info">
                <p>You need to upgrade to a paid organization to invite more admins. Visit the <a href="/billing/manage">Billing</a> section to upgrade.</p>
            </Alert>
        }
        else
        {
            <EditForm class="space-y-4" FormName="@InviteFormName" Model="InviteForm" OnValidSubmit="@OnValidInviteAsync">
                <DataAnnotationsValidator />
                <ValidationSummary />
                <CustomValidationErrors EditContext="InviteFormEditContext" />
                <div class="mb-4 flex">
                    <div class="relative flex flex-grow items-stretch focus-within:z-10  max-w-md">
                        <InputText @bind-Value="InviteForm.Email" placeholder="recipient@example.org" class="block w-full rounded-none rounded-l-md border-0 py-2 px-2 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:text-sm sm:leading-6"/>
                    </div>
                    <button type="submit" class="btn-primary inline rounded-l-none">
                        Send invite
                    </button>
                </div>
            </EditForm>
        }
        <EditForm class="space-y-4" FormName="@CancelInviteFormName" Model="CancelInviteForm" OnSubmit="@CancelInviteAsync">
            <DataAnnotationsValidator />
            <ValidationSummary />
            <CustomValidationErrors EditContext="CancelInviteFormEditContext" />
            <Table EmptyMessage="@InvitationsEmptyMessage" ColumnHeaders="_invitationColumnHeaders">
                @if (Invites != null)
                {
                    @foreach (var inv in Invites)
                    {
                        <tr>
                            <td class="whitespace-nowrap">@inv.ToEmail</td>
                            <td class="whitespace-nowrap"><LocalDateTime Value="@inv.CreatedAt"/></td>
                            <td class="whitespace-nowrap">Sent</td>
                            <td class="whitespace-nowrap">@inv.FromName</td>
                            <td class="whitespace-nowrap">
                                <button class="btn-secondary" name="CancelInviteForm.HashedCode" value="@inv.HashedCode" type="submit">Cancel</button>
                            </td>
                        </tr>
                    }
                }
            </Table>
        </EditForm>
    </Panel>
</Page>